package org.advanced.extend;

/**
 * @author John.zhang
 * @version Id: ThreadJoin, v 1.0 2020/12/7 15:03 ZhiYong Exp $
 */
public class ThreadJoin {

    private static int x = 0;
    private static int i = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            i = 1;
            x = 2;
        });

        Thread thread2 = new Thread(() -> {
            i = x + 2;
        });
        thread1.start();
        //保证线程的可见性 --阻塞
        thread1.join();
        //上面等同于下面这种
        new ThreadJoin().join();
        thread2.start();
        Thread.sleep(1000);
        System.out.println(i);
    }


    public  final synchronized void join(){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
